#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
HOME_PATH=`cd ~; pwd`
cocostudioPath="$HOME_PATH/work/qunying/share/trunk/UI/qunying"
lzgamePath="$HOME_PATH/work/qunying/client/trunk/gameClient"

if [ $# -gt 1 ] ; then
	cocostudioPath=$1
	lzgamePath=$2
fi

#please modify the lzgamePath

TEMP_DIR=$HOME/work/tmp
if [ -d /Volumes/RamDisk ];then
    TEMP_DIR=/Volumes/RamDisk
fi
cd $DIR
#cocostudioPath=$lzgamePath'/cached_res/image'
scriptPath=$lzgamePath'/cached_res/script'
moConfigPath=$lzgamePath'/cached_res/script/mvc/instance/view'
luaPath=$DIR'/script/oyeahgame'
svn update $DIR
svn update $DIR'/config'
svn commit $DIR'/config' -m 'auto commit config'
svn update $DIR'/template'
svn commit $DIR'/template' -m 'auto commit template'
svn update $cocostudioPath
svn commit $cocostudioPath -m 'auto commit config'
svn update $scriptPath
echo ''
java -jar -Dencoding=gb2312 -Dfile.encoding=UTF-8 -Duser.language=zh -Duser.region=CN JsonToLua.jar $cocostudioPath/cocosstudio

luaPathFileList=`ls $luaPath`
for funcName in $luaPathFileList
do
	echo ''
	echo 'moduleName='$funcName
	controlPath=$luaPath'/'$funcName'/control'
	logicPath=$luaPath'/'$funcName'/logic'
	
	modulePath=$scriptPath'/oyeahgame/'$funcName
	targetConfigPath=$scriptPath'/oyeahgame/'$funcName'/control'
	targetLogicPath=$scriptPath'/oyeahgame/'$funcName'/logic'
	
	if [ ! -d "$modulePath" ]; then 
		mkdir "$modulePath" 
	fi
	if [ ! -d "$targetConfigPath" ]; then 
		mkdir "$targetConfigPath" 
	fi
	if [ ! -d "$targetLogicPath" ]; then 
		mkdir "$targetLogicPath" 
	fi
	
	echo 'copy  to '$targetConfigPath
	echo 'copy  to '$targetLogicPath
	cp $controlPath/*.lua   $targetConfigPath
	cp $logicPath/*.eg      $targetLogicPath
done

echo ''
moduleConfigPath=$DIR'/script/mvc/instance/view/*.lua'
if [ ! -d "$moConfigPath" ]; then 
	mkdir "$moConfigPath" 
fi
echo ''
echo 'copy to '$moConfigPath
cp $moduleConfigPath     $moConfigPath

svn  add $moConfigPath/*Auto.lua
svn  commit -m "commit, json to lua code auto, the moduleConfigAuto..." $moConfigPath/*Auto.lua

pack_temImage(){
TexturePacker $TEMP_DIR/tempimage \
--enable-rotation \
--smart-update \
--max-size 2048 \
--png-opt-level 7 \
--trim \
--allow-free-size \
--dither-fs-alpha \
--size-constraints $1 \
--format cocos2d \
--shape-padding 2 \
--border-padding 0 \
--padding 0 \
--algorithm MaxRects \
--opt $4 \
--scale 1 \
--texture-format pvr2ccz \
--sheet $2 \
--data $TEMP_DIR/spriteSheetName.plist  
tr -d '\n' <$TEMP_DIR/spriteSheetName.plist|sed 's/> [ ]*</></g'|sed 's/  [ ]*/ /g' | sed 's/<string>$TexturePacker[^<]*<\/string>/<string><\/string>/g'  |sed 's/\n//g' | sed 's/<!DOCTYPE[^>]*>//g' > $3

}

#echo ''
#svn add $scriptPath --force
#echo 'svn commit to '$scriptPath
#svn commit -m "commit json to lua code auto" $scriptPath

echo ''
echo "commit logic and control..."
oyeahgamePath=$lzgamePath'/cached_res/script/oyeahgame'
oyeahgameFileList=`ls $oyeahgamePath`
for funcName in $oyeahgameFileList
do
    targetConfigPath=$scriptPath'/oyeahgame/'$funcName'/control'
    targetLogicPath=$scriptPath'/oyeahgame/'$funcName'/logic'
    if [ -d "$targetLogicPath" ]; then
        echo 'commit module, moduleName='$funcName
        tempPath=$scriptPath'/oyeahgame/'$funcName
        svn add $tempPath --force
        svn commit -m "commit, json to lua code auto, the module package..." $tempPath
        svn add $targetConfigPath/*.lua --force
        svn add $targetLogicPath/*.eg --force
        svn commit -m "commit, json to lua code auto, the *Control.lua" $targetConfigPath/*.lua
        svn commit -m "commit, json to lua code auto, the *Logic.eg"     $targetLogicPath/*.eg
    fi
done
svn update $lzgamePath'/cached_res/image/'
cp -rf $cocostudioPath'/cocosstudio/image' $lzgamePath'/cached_res/'
rm -rf $lzgamePath'/cached_res/image/hero/halfbody/*.png'
rm -rf $lzgamePath'/cached_res/*.ttf'
rm -rf $lzgamePath'/cached_res/fonts'
rm -rf $lzgamePath'/cached_res/font'

rm -rf $cocostudioPath'/res/image'
rm -rf $cocostudioPath'/res/Default'
cp -rf $cocostudioPath'/res/' $lzgamePath'/cached_res/image/'
rm -rf $cocostudioPath'/res/'
rm -rf $lzgamePath/cached_res/image/ui/*
rm -rf $TEMP_DIR/tempimage
mkdir $TEMP_DIR/tempimage
mkdir $TEMP_DIR/tempimage/image
mkdir $TEMP_DIR/tempimage/image/ui
rm $cocostudioPath/cocosstudio/image/ui/login/jdt-*.png
rm $cocostudioPath/cocosstudio/image/ui/login/dldt.jpg
rm $cocostudioPath/cocosstudio/image/ui/login/yjad.png
rm $cocostudioPath/cocosstudio/image/ui/login/dldtbg.png
rm $cocostudioPath/cocosstudio/image/ui/login/logo.png
rm $cocostudioPath/cocosstudio/image/ui/login/ao_01.png
rm $cocostudioPath/cocosstudio/image/ui/login/ao_02.png

for i in $cocostudioPath/cocosstudio/image/ui/* ; do
    if [ -d $i ] ; then
        rm -rf $TEMP_DIR/tempimage/image/ui/*
        spriteSheetName=`basename $i`
        cp -rf $cocostudioPath/cocosstudio/image/ui/$spriteSheetName $TEMP_DIR/tempimage/image/ui/
        # find $TEMP_DIR/tempimage/image/ui -type f -name "*.jpg" -exec rm -rf {} \;
        if [ "Common" = "$spriteSheetName" ];then
        	pack_temImage "NPOT" $lzgamePath/cached_res/image/ui/$spriteSheetName.pvr.ccz  $lzgamePath/cached_res/image/ui/$spriteSheetName.plist 'RGBA8888'
        elif [ "homemain" = "$spriteSheetName" ];then
        	pack_temImage "NPOT" $lzgamePath/cached_res/image/ui/$spriteSheetName.pvr.ccz  $lzgamePath/cached_res/image/ui/$spriteSheetName.plist 'RGBA8888'
        else
        	pack_temImage "NPOT" $lzgamePath/cached_res/image/ui/$spriteSheetName.pvr.ccz  $lzgamePath/cached_res/image/ui/$spriteSheetName.plist 'RGBA8888'
        fi
    fi
done

for i in $cocostudioPath/cocosstudio/image/*-* ; do
    if [ -d $i ] ; then
        LOCALETYPE=`basename $i`
        rm $cocostudioPath/cocosstudio/image/$LOCALETYPE/ui/login/jdt-*.png
		rm $cocostudioPath/cocosstudio/image/$LOCALETYPE/ui/login/dldt.jpg
		rm $cocostudioPath/cocosstudio/image/$LOCALETYPE/ui/login/yjad.png
		rm $cocostudioPath/cocosstudio/image/$LOCALETYPE/ui/login/dldtbg.png
		rm $cocostudioPath/cocosstudio/image/$LOCALETYPE/ui/login/logo.png
		rm $cocostudioPath/cocosstudio/image/$LOCALETYPE/ui/login/ao_01.png
		rm $cocostudioPath/cocosstudio/image/$LOCALETYPE/ui/login/ao_02.png
        if [ -d $i/ui ] ; then
	        for ii in $i/ui/* ; do
			    if [ -d $ii ] ; then
			        spriteSheetName=`basename $ii`
			        rm -rf $TEMP_DIR/tempimage/image/ui/*
			        cp -rf $cocostudioPath/cocosstudio/image/ui/$spriteSheetName $TEMP_DIR/tempimage/image/ui/
			        cp -rf $ii $TEMP_DIR/tempimage/image/ui/
			        rm -rf $lzgamePath/cached_res/image/$LOCALETYPE/ui/$spriteSheetName
			        if [ "Common" = "$spriteSheetName" ];then
			        	pack_temImage "NPOT" $lzgamePath/cached_res/image/$LOCALETYPE/ui/$spriteSheetName.pvr.ccz  $lzgamePath/cached_res/image/$LOCALETYPE/ui/$spriteSheetName.plist 'RGBA8888'
			        elif [ "homemain" = "$spriteSheetName" ];then
			        	pack_temImage "NPOT" $lzgamePath/cached_res/image/$LOCALETYPE/ui/$spriteSheetName.pvr.ccz  $lzgamePath/cached_res/image/$LOCALETYPE/ui/$spriteSheetName.plist 'RGBA8888'
			        else
			        	pack_temImage "NPOT" $lzgamePath/cached_res/image/$LOCALETYPE/ui/$spriteSheetName.pvr.ccz  $lzgamePath/cached_res/image/$LOCALETYPE/ui/$spriteSheetName.plist 'RGBA8888'
			        fi 
			    fi
			done
		fi
    fi
done

cd $lzgamePath'/cached_res/image/'
rm -rf hero/halfbody/*.png
svn st | grep '^\?' | tr '^\?' ' ' | sed 's/[ ]*//' | sed 's/[ ]/\\ /g' | xargs svn add

svn commit -m "commit,cocosstudio res"
svn up 
echo 'success'